package com.lkd.job;


import cn.hutool.Hutool;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.lkd.domain.Area;
import com.lkd.domain.City;
import com.lkd.domain.Province;
import com.lkd.domain.Xian;
import com.lkd.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 该类将会在项目启动的时候读取省市区的文件，并将数据存放进area表中
 */
//@Component废弃

public class InitAreas {
    public final String NINGNING = "00";
    @Autowired
    private IAreaService areaService;

    @PostConstruct
    public void init() throws IOException {

        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ssq.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder stringBuilder = new StringBuilder("");
        String line;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
        }
        JSONArray jsonArray = JSONUtil.parseArray(stringBuilder);
        List<Province> list = jsonArray.toList(Province.class);
        List<Area> areas = BeanUtil.copyToList(list, Area.class);
        areas.stream().forEach(s->s.setAreaLevel("1"));
        areaService.saveBatch(areas);
        for (Province province : list) {
            City[] city = province.getCity();
            List<Area> citys = Arrays.stream(city).map(c -> {
                Area area = setInfo(c.getCode(), c.getName());
                return area;
            }).collect(Collectors.toList());
            areaService.saveBatch(citys);
            for (City city1 : city) {
                Xian[] xians = city1.getArea();
                List<Area> collect = Arrays.stream(xians).map(x -> {
                    Area xian = setInfo(x.getCode(), x.getName());
                    return xian;
                }).collect(Collectors.toList());
                areaService.saveBatch(collect);
            }
        }
    }



    /**
     * 该方法传入区域编号，并且返回一个Area类型的对象
     *
     * @param code,name
     */
    private Area setInfo(String code,String name) {
        Area area = new Area();
        area.setCode(Integer.parseInt(code));
        area.setAreaLevel(getLevel(code).toString());
        //生成该区域的父编号
        StringBuilder fatherCode = new StringBuilder("");
        if ("2".equals(area.getAreaLevel())){
            //该区域为市
            fatherCode =  new StringBuilder(code.substring(0, 2)+NINGNING+NINGNING);
        }else {
            //该区域为区县
            fatherCode =  new StringBuilder(code.substring(0, 4)+NINGNING);
        }
        area.setParentId(Integer.parseInt(fatherCode.toString()));
        return area;
    }


    /**
     * 该方法传入区域编号，返回区域级别
     *
     * @param code
     * @return
     */
    private Integer getLevel(String code) {

        char[] charArray = code.toCharArray();
        StringBuilder start = new StringBuilder(charArray[0] + charArray[1]);
        StringBuilder mid = new StringBuilder(charArray[2] + charArray[3]);
        StringBuilder end = new StringBuilder(charArray[4] + charArray[5]);
        if (NINGNING.equals(end)) {
            return 2;
        }else {
            return 3;
        }
    }
}